--[[
	Exsto
	Copyright (C) 2010  Prefanatic

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
]]


-- Printing Utilities.

-- Variables

exsto.PrintStyles = {}
exsto.TextStart = "[Exsto] "
exsto.ErrorStart = "[EXSTO ERROR]"
exsto.DebugStart = "[ExDebug] "

--[[ -----------------------------------
	Function: AddPrint
	Description: Adds printing styles.
     ----------------------------------- ]]
local function AddPrint( func, ply ) -- Func args depend on called.
	table.insert( exsto.PrintStyles, { enum = #exsto.PrintStyles + 1, func = func, meta = ply } )
	return #exsto.PrintStyles
end

exsto_CHAT = AddPrint( 
	function( ply, ... )
		if {...} == nil then return end
		local arg = {...}
		
		-- Need to format for Minecraft
		local str, item = "", nil
		for I = 1, #arg do
			item = arg[I]
			
			if type( item ) == "string" then
				str = str .. item
			elseif type( item ) == "number" then
				str = str .. tostring( item ) .. "^" -- Colour Codes :]
			else
				str = str .. "[" .. tostring( item ) .. "]"
			end
		end
		
		if type( ply ) == "string" and ply == "all" then
			for _, ply in pairs( Server.GetPlayers( "#all" ) ) do
				ply:Print( str )
			end
		else
			ply:Print( str )
		end
		
	end, true
)
exsto_CHAT_NOLOGO = exsto_CHAT
	
exsto_CHAT_LOGO = AddPrint( 
	function( ply, ... )
		exsto.Print( exsto_CHAT, ply, "[Exsto] ", unpack( {...} ) )
	end, true
)
	
exsto_CHAT_ALL = AddPrint( 
	function( ... )
		exsto.Print( exsto_CHAT, "all", unpack( {...} ) )
	end
)

exsto_CHAT_ALL_LOGO = AddPrint(
	function( ... )
		exsto.Print( exsto_CHAT_LOGO, "all", unpack( ... ) )
	end
)
	
exsto_CONSOLE = AddPrint( 
	function( msg, extra )
		print( msg )
	end
)
exsto_CONSOLE_NOLOGO = exsto_CONSOLE

exsto_CONSOLE_LOGO = AddPrint(
	function( msg, extra )
		print( exsto.TextStart .. msg )
	end
)
	
exsto_CONSOLE_DEBUG = exsto_CONSOLE
	
exsto_ERROR = AddPrint( 
	function( msg )
		local send = exsto.ErrorStart .. " " .. msg .. "\n" 
		
		--debug.Trace()
		error( send )
	end
)

exsto_ERRORNOHALT = AddPrint( 
	function( msg )
		local send = exsto.ErrorStart .. " " .. msg .. "\n" 

		Server.Warning( send )
	end
)
	
--[[exsto_CLIENT = AddPrint( 
	function( ply, msg )
		if CLIENT then return end
		if type( ply ) != "Player" and type( ply ) != "table" then -- It seems like we are going console.
			exsto.Print( exsto_CONSOLE, msg )
			return
		end
		
		-- Check if we should use our UMSG system.
		if msg:len() > 200 then
		
			local sender = exsto.CreateSender( "ExClientPrint", ply )
				sender:AddString( msg )
			sender:Send()
			
		else
			ply:PrintMessage( HUD_PRINTCONSOLE, msg )
		end
		
	end, true
)
exsto_CLIENT_NOLOGO = exsto_CLIENT
	
exsto_CLIENT_LOGO = AddPrint( 
	function( ply, msg )
		if CLIENT then return end
		exsto.Print( exsto_CLIENT, ply, exsto.TextStart .. " " .. msg .. "\n" )
	end, true
)

exsto_CLIENT_ALL = AddPrint( 
	function( msg )
		if CLIENT then return end
		for _, ply in ipairs( player.GetAll() ) do
			exsto.Print( exsto_CLIENT, ply, msg )
		end
	end
)

exsto_CLIENT_ALL_LOGO = AddPrint( 
	function( msg )
		if CLIENT then return end
		for _, ply in ipairs( player.GetAll() ) do
			exsto.Print( exsto_CLIENT_LOGO, ply, msg )
		end
	end
)]]

exsto_DEBUG = AddPrint(
	function( msg, level )
		print( exsto.DebugStart .. msg )
	end
)
--[[
exsto_FILE_APPEND = AddPrint(
	function( msg )
		local d = os.date( "%m-%d-%y" )
		local f = file.Read( "exsto_fappend_" .. d .. ".txt" )
		file.Write( "exsto_fappend_" .. d .. ".txt", f + .. "\n" .. msg )
	end
)]]
	
--[[ -----------------------------------
	Function: exsto.Print
	Description: Prints a specific style.
     ----------------------------------- ]]
function exsto.Print( style, ... )
	if exsto.Ignore_Prints then return true end
	if style == nil then return end
	for k,v in pairs( exsto.PrintStyles ) do
		if style == v.enum then	
			--hook.Call( "ExPrintCalled", nil, style, {...} )
			return v.func( ... )		
		end	
	end
end

--[[ -----------------------------------
	Function: exsto.IgnorePrints
	Description: Makes all prints ignored as a toggle.
     ----------------------------------- ]]
function exsto.IgnorePrints( ignore )
	exsto.Ignore_Prints = ignore
end

-- Helper Functions
function exsto.Error( msg )
	exsto.Print( exsto_ERROR, msg )
end

function exsto.ErrorNoHalt( msg )
	exsto.Print( exsto_ERRORNOHALT, msg )
end

function exsto.Debug( msg, level )
	exsto.Print( exsto_DEBUG, msg, level )
end